package dcedit.util;

// Import Java SDK Classes
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;

// Import Application Classes
import dcedit.*;

/** ImageSpinner class */
public class ImageSpinner extends JPanel {
   /** Serializable constant */
   public static final long serialVersionUID = 1287967332592L;

   /** Image detail field */
   private OmniImage image = new OmniImage("");

   /** Image list field */
   private List<OmniImage> imageList;

   /** Current image index field */
   private int currentImageIndex = 0;

   /** Constructor */
   public ImageSpinner() {
      this(new ArrayList<OmniImage>());
   } // END constructor

   /** Constructor */
   public ImageSpinner(OmniImage[] imageArray) {
      this(Arrays.asList(imageArray));
   } // END constructor

   /** Constructor */
   public ImageSpinner(List<OmniImage> imageList) {
      super(new BorderLayout());
      if(null != imageList) this.imageList = imageList;
      else this.imageList = new ArrayList<OmniImage>();
      init();
   } // END constructor

   /** init method */
   private void init() {
      if(!imageList.isEmpty()) image.setIcon(imageList.get(currentImageIndex).getIcon());

      image.setBorder(BorderFactory.createLineBorder(Colors.getAppBackgroundColor()));
      image.setHorizontalAlignment(OmniImage.CENTER);
      image.setVerticalAlignment(OmniImage.CENTER);

      this.add(initLeftSpinnerBtn(), BorderLayout.WEST);
      this.add(image, BorderLayout.CENTER);
      this.add(initRightSpinnerBtn(), BorderLayout.EAST);
   } // END init

   /** initLeftSpinnerBtn method */
   private JButton initLeftSpinnerBtn() {
      JButton leftSpinner = new JButton(Constants.LESS_THAN_CHAR);

      leftSpinner.setMargin(new Insets(0, 0, 0, 0));;

      leftSpinner.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            if(!imageList.isEmpty()) {
               --currentImageIndex;
               if(currentImageIndex < 0) currentImageIndex = imageList.size() - 1;
               image.setIcon(imageList.get(currentImageIndex).getIcon());
            } // END if
         } // END actionPerformed
      });

      return leftSpinner;
   } // END initLeftSpinnerBtn

   /** initRightSpinnerBtn method */
   private JButton initRightSpinnerBtn() {
      JButton rightSpinner = new JButton(Constants.GREATER_THAN_CHAR);

      rightSpinner.setMargin(new Insets(0, 0, 0, 0));

      rightSpinner.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            if(!imageList.isEmpty()) {
               ++currentImageIndex;
               if(currentImageIndex >= imageList.size()) currentImageIndex = 0;
               image.setIcon(imageList.get(currentImageIndex).getIcon());
            } // END if
         } // END actionPerformed
      });

      return rightSpinner;
   } // END initRightSpinnerBtn

   /** setSpinnerBtnWidth method */
   public void setSpinnerBtnWidth(int width) {
      int[] componentIndexes = {0,2};
      for(int componentIndex : componentIndexes) {
         JButton spinner = (JButton)this.getComponent(componentIndex);
         spinner.setSize(
            new Dimension(width,spinner.getSize().height)
         );
         spinner.setMinimumSize(
            new Dimension(width,spinner.getMinimumSize().height)
         );
         spinner.setPreferredSize(
            new Dimension(width,spinner.getPreferredSize().height)
         );
         spinner.setMaximumSize(
            new Dimension(width,spinner.getMaximumSize().height)
         );
      } // END loop
   } // END setSpinnerBtnWidth

   /** getCurrentlySelectedImage method */
   public OmniImage getCurrentlySelectedImage() {
      return image;
   } // END getCurrentlySelectedImage

   public OmniImage getSelectedImage() {
      return getCurrentlySelectedImage();
   } // END getSelectedImage

   /** setSelectedImage method */
   public void setSelectedImage(int index) {
      if(null != imageList && 0 < index && index < imageList.size()) {
         currentImageIndex = index;
         image.setIcon(imageList.get(currentImageIndex).getIcon());
      } // END if
   } // ENd setSelectedImage

   /** setSelectedImage method */
   public void setSelectedImage(OmniImage image) {
      this.image.setIcon(image.getIcon());
   } // END setSelectedImage

   /** main method */
   public static void main(String[] args) {
      JFrame testFrame = new JFrame("ImageSpinner Test");

      OmniImage[] imageArray = {
         new OmniImage(DCEdit.class.getResource("/dcedit/img/add-48.png")),
         new OmniImage(DCEdit.class.getResource("/dcedit/img/folder-48.png")),
         new OmniImage(DCEdit.class.getResource("/dcedit/img/save-48.png")),
         new OmniImage(DCEdit.class.getResource("/dcedit/img/cancel-48.png"))
      };

      testFrame.getContentPane().add(new ImageSpinner(imageArray));
      testFrame.pack();
      testFrame.setLocationRelativeTo(null);
      testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      testFrame.setVisible(true);
   } // END static main
} // END class ImageSpinner